﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using HostTool.Util;

namespace HostTool.Core
{
    class HostParamFirmType:FirmwareType
    {

        public HostParamFirmType(string name, byte type)
            : base(name, type)
        {
        }

        public override byte[] Convert(byte[] fileData)
        {
            MemoryStream ms = new MemoryStream(fileData);
            MemoryStream msArray = new MemoryStream();
            IniDocument iniDoc = new IniDocument(ms);

            BinaryWriter bw = new BinaryWriter(msArray);
            int[] valList = null;
            //[PARA方式]
            string sectionName = "PARA方式";
            int val = iniDoc.GetInt(sectionName, "参数方式", 0xaaaa);
            bw.Write((UInt16)val);

            //[ZPW2000]
            sectionName = "ZPW2000";
            val = iniDoc.GetInt(sectionName, "解码允许", 1);
            bw.Write((UInt16)val);
            valList = iniDoc.GetIntList(sectionName, "通道增益");
            for (int i = 0; i < 4; i++)
            {
                val = 0;
                if (valList != null && valList.Length > i)
                {
                    val = valList[i];
                }
                bw.Write((UInt16)val);
            }
            val = iniDoc.GetInt(sectionName, "掉灯延时", 3700);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "载波锁定延时", 15000);
            bw.Write((UInt16)val);


            //[国产移频]
            sectionName = "国产移频";
            val = iniDoc.GetInt(sectionName, "解码允许", 1);
            bw.Write((UInt16)val);
            valList = iniDoc.GetIntList(sectionName, "通道增益");
            for (int i = 0; i < 4; i++)
            {
                val = 0;
                if (valList != null && valList.Length > i)
                {
                    val = valList[i];
                }
                bw.Write((UInt16)val);
            }
            val = iniDoc.GetInt(sectionName, "掉灯延时", 3700);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "线路方式", 0);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "点灯方式", 0);
            bw.Write((UInt16)val);


            //[交流计数]
            sectionName = "交流计数";
            valList = iniDoc.GetIntList(sectionName, "解码允许");
            for (int i = 0; i < 2; i++)
            {
                val = 0;
                if (valList != null && valList.Length > i)
                {
                    val = valList[i];
                }
                bw.Write((UInt16)val);
            }
            valList = iniDoc.GetIntList(sectionName, "通道增益");
            for (int i = 0; i < 2; i++)
            {
                val = 0;
                if (valList != null && valList.Length > i)
                {
                    val = valList[i];
                }
                bw.Write((UInt16)val);
            }
            val = iniDoc.GetInt(sectionName, "掉灯延时1", 8700);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "掉灯延时2", 6700);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "点灯方式", 0);
            bw.Write((UInt16)val);
            //[单轨条]
            sectionName = "单轨条";
            val = iniDoc.GetInt(sectionName, "解码允许", 0);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "通道增益", 100);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "掉灯延时", 3700);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "载波锁定延时", 15000);
            bw.Write((UInt16)val);
            //[极频]
            sectionName = "极频";
            val = iniDoc.GetInt(sectionName, "解码允许", 0);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "通道增益", 100);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "掉灯延时", 3700);
            bw.Write((UInt16)val);

            //[TVM430]
            sectionName = "TVM430";
            val = iniDoc.GetInt(sectionName, "解码允许", 0);
            bw.Write((UInt16)val);
            valList = iniDoc.GetIntList(sectionName, "通道增益");
            for (int i = 0; i < 4; i++)
            {
                val = 0;
                if (valList != null && valList.Length > i)
                {
                    val = valList[i];
                }
                bw.Write((UInt16)val);
            }
            val = iniDoc.GetInt(sectionName, "掉灯延时", 3700);
            bw.Write((UInt16)val);


            //[传感器特征码]
            sectionName = "传感器特征码";
            val = iniDoc.GetInt(sectionName, "频率", 10000);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "幅度条件", 100);
            bw.Write((UInt16)val);
            val = iniDoc.GetInt(sectionName, "延时条件", 5000);
            bw.Write((UInt16)val);

            byte[] dataParam = msArray.ToArray();

            UInt16 crc = CRC16.ComputeCRC16(dataParam, 0, dataParam.Length);
           
            bw.Write((byte)((crc>>8) & 0xff));
            bw.Write((byte)(crc & 0xff));
            bw.Flush();
            return msArray.ToArray();

        }
    }
}
